#include "logger.h"

static void SoftWareShutDown ()
{
    if (NULL != q_fileStream)
    {
        qDebug("Close log file.");
        fclose (q_fileStream);
        q_fileStream = NULL;
    }
}

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    Q_UNUSED(type);
    QByteArray localMsg = msg.toLocal8Bit();
    if (g_logLevel && q_fileStream)
    {
        int iLevel = g_TraceLevel[type];
        if (g_logLevel < iLevel)
        {
            return;
        }

        fprintf (q_fileStream, "%s: %s (%s:%u, %s)\n", _TIME_, localMsg.constData(), context.file, context.line, context.function);
        fflush (q_fileStream);
    }
    /*
    switch (type) {
    case QtDebugMsg:
        fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtInfoMsg:
        fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtWarningMsg:
        fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtCriticalMsg:
        fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtFatalMsg:
        fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        abort();
    }
    */
}

static void installMsgHandler(QString strLogFile)
{
    QByteArray byteArrayLogFile = strLogFile.toLatin1();

    if (NULL != q_fileStream)
    {
        fclose (q_fileStream);
        q_fileStream = NULL;
    }
    qDebug()<<"byteArrayLogFile = "<<byteArrayLogFile;

    QString strAppPath = QCoreApplication::applicationDirPath();
    QString strIniFile = strAppPath + "/RunInfo.ini";
    QSettings pSettings(strIniFile, QSettings::IniFormat);
    QByteArray DebugMsgShowWay = pSettings.value("DebugMsgShowWaySet/DebugMsgShowWay", 1).toString().toLatin1();



    q_fileStream = fopen (byteArrayLogFile.constData (), DebugMsgShowWay.constData ());
    if (NULL != q_fileStream)
    {
        qDebug("Opened log file.");
        qInstallMessageHandler(myMessageOutput);
    }
    else
    {
        qDebug("Failed to open log file.");
    }
}

static void installReleaseMsgHandler ()
{
    QString strAppPath = QCoreApplication::applicationDirPath();
    //qDebug()<<"strAppPath="<<strAppPath;
    QString strIniFile = strAppPath + "/RunInfo.ini";
    QString strLogFile = strAppPath + "/log.txt";
    qDebug()<<"strIniFile="<<strIniFile;
    qDebug()<<"strLogFile="<<strLogFile;
    QSettings pSettings(strIniFile, QSettings::IniFormat);
    g_logLevel = pSettings.value("TraceSet/TraceLevel", 1).toInt();
    installMsgHandler (strLogFile);
}

void logger_init()
{
#ifndef QT_DEBUG
    bool bAddQuitFunc = false;
    installReleaseMsgHandler();
    if (!bAddQuitFunc)
    {
        qAddPostRoutine (SoftWareShutDown);
        bAddQuitFunc = true;
    }
#endif
}
